package jdk8_new_date_api;

import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Java8 新时间api
 * @author zc
 *
 */
public class Demo {
	
	public static void main(String[] args) {
		api_LocalDate();
		System.out.println("----------------------");
		api_LocalTime();
		System.out.println("----------------------");
		api_LocalDateTime();
		
	}
	
	private static void api_LocalDateTime() {
		LocalDateTime localDateTime = LocalDateTime.now();
		ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("America/New_York"));
		
		System.out.println(localDateTime);
		System.out.println(zonedDateTime);
	}

	private static void api_LocalTime() {
		LocalTime localTime = LocalTime.now();
		LocalTime localTime1 = LocalTime.ofInstant(Instant.now(), ZoneId.systemDefault());
		LocalTime localTime2 = LocalTime.of(8, 46);
		LocalTime localTime3 = LocalTime.of(20, 46);
		LocalTime ofSecondOfDay = LocalTime.ofSecondOfDay(61);
		
		System.out.println(localTime);
		System.out.println(localTime.toString());
		System.out.println(localTime1);
		System.out.println(localTime2);
		System.out.println(localTime3);
		System.out.println(ofSecondOfDay);
	}

	private static void api_LocalDate() {
		LocalDate localDate = LocalDate.EPOCH; // 新纪元 1970-01-01
		LocalDate localDate0 = LocalDate.ofInstant(Instant.now(), ZoneId.systemDefault());
		LocalDate localDate1 = LocalDate.now(); // 2021-03-11
		LocalDate localDate2 = LocalDate.of(2021, 3, 11); // 2021-03-11
		LocalDate localDate3 = LocalDate.parse("2021-03-11");
		LocalDate localDate4 = LocalDate.parse("2021-03-11", DateTimeFormatter.ISO_LOCAL_DATE);
		LocalDate localDate5 = LocalDate.parse("20210311", DateTimeFormatter.BASIC_ISO_DATE);
		
		System.out.println(localDate0);
		System.out.println(localDate1);
		System.out.println(localDate2);
		System.out.println(localDate3);
		System.out.println(localDate4);
		System.out.println(localDate5);
		
		LocalDateTime atStartOfDay = localDate5.atStartOfDay(); // 2021-03-11T00:00
		System.out.println(atStartOfDay);
		
		int dayOfMonth = localDate5.getDayOfMonth(); // 11
		System.out.println(dayOfMonth); 
		
		DayOfWeek dayOfWeek = localDate5.getDayOfWeek(); // THURSDAY
		System.out.println(dayOfWeek);
		
		int dayOfYear = localDate5.getDayOfYear(); // 70
		System.out.println(dayOfYear);
		
		Month month = localDate5.getMonth(); // MARCH
		System.out.println(month);
	}

}
